package com.yixq.soa.service.user.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.core.io.support.ResourcePatternResolver;
//import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
//import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import springfox.documentation.annotations.ApiIgnore;
//import springfox.documentation.spring.web.DocumentationCache;
//import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper;
/**
 * 
* @ClassName: SwaggerController
* @Description: 用于显示swagger ui 的控制器.
* @author kyler.tang 
* @date 2016年11月10日 下午9:11:34
*
 */
@Controller
@Profile({"dev","prod"})
@ApiIgnore
public class SwaggerController {
	
//	@Autowired
//	private DocumentationCache documentationCache;
	
//	@Autowired
//	private ServiceModelToSwagger2Mapper mapper;
	
//	private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	
//	private MetadataReaderFactory metadataReaderFactory =
//			new CachingMetadataReaderFactory(this.resourcePatternResolver);
//	
	protected String resolveBasePackage(String basePackage) {
		return ClassUtils.convertClassNameToResourcePath(basePackage);
	}
	
	@RequestMapping("/apis/index.html")
	// @Permission("")
	public String swagger_ui(HttpServletRequest request, ModelMap model) throws IOException {
		//Lists.newArrayList("MOCK_SUPER_")
		//453l,"admin"
//////  类扫描方法
//		String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
//				resolveBasePackage("com.doordu.admin.web.controller") + "/" + "**/*.class";
//		Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
//		for (Resource resource : resources) {
//			if (resource.isReadable()) {
//				try {
//					MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
//					Class<?> clazz = metadataReader.getClass();
//					if(clazz != null){
////						Permission t_permission = AnnotationUtils.findAnnotation(method.getBeanType(), Permission.class);
////						Permission m_permission = AnnotationUtils.findAnnotation(method.getMethod(), Permission.class);
////						ApiOperation apioperation = AnnotationUtils.findAnnotation(method.getMethod(), ApiOperation.class);
//					}
//				}catch(Exception exx){
//					exx.printStackTrace();
//				}
//			}
//		}
//		
//		//获取swagger最终生成的对象,这个对象直接json就是ui所使用的
//		String groupName = Docket.DEFAULT_GROUP_NAME;
//	    Documentation documentation = documentationCache.documentationByGroup(groupName);
//
//	    Swagger swagger = mapper.mapDocumentation(documentation);
//		
		//HttpSession session = request.getSession(false);
		
//		Object mu = session.getAttribute(AuthUtils.USER_REALM_KEY);
//		if(mu == null){
//			UserRealm mockuser = new UserRealm();
//			mockuser.setUserId(1l);
//			mockuser.setUserName("admin");
//			mockuser.setPermissions(Lists.newArrayList("MOCK_SUPER_"));
//			mockuser.setRoleList(null);
//
//			session.setAttribute(AuthUtils.USER_REALM_KEY, mockuser);
//		}
		
		model.put("apiPath", request.getContextPath()+"/v2/api-docs");
		return "swagger-ui";
	}
}
